'''
Case Type： 数据库参数
Case Name： 设置synchronous_commit参数的值为off，most_available_sync的值为on
Create At:  2020/07/22
Owner    :  @li-xin12345
Descption:  1. 更改参数值 2. 重启数据库使参数生效 3. 在主机上做DDL操作 4. 在备机上查询数据
'''

import os
import sys
import unittest
import time
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class IndexFileDamaged(unittest.TestCase):
    logger.info('------------Opengauss_Reliability_Dbsys_Case212.py start------------')
    rootNode = Node('PrimaryRoot')
    primaryNode = Node(node='PrimaryDbUser')
    standbyNode = Node(node='Standby1DbUser')
    DB_ENV_PATH = macro.DB_ENV_PATH
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    Constant = Constant()
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def test_Index_file_damaged(self):
        # 更改参数值
        logger.info('----------------------------更改参数值-----------------------------')
        mod_msg = self.sh_primy.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'synchronous_commit = off')
        self.assertTrue(mod_msg)
        mod_msg1 = self.sh_primy.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'most_available_sync=on')
        self.assertTrue(mod_msg1)

        # 重启数据库使参数生效
        logger.info('----------------------------重启数据库使参数生效-----------------------------')
        is_stoped = self.sh_primy.stopDbCluster()
        self.assertTrue(is_stoped)
        logger.info(f'{is_stoped}')
        is_started = self.sh_primy.startDbCluster()
        self.assertTrue(is_started)
        logger.info(f'{is_started}')

        # 做DDL操作建表
        logger.info('----------------------------做DDL操作创建表-----------------------------')
        sql_cmd = '''
                        drop table if exists test_zl;
                        create table test_zl(a tinyint,b tinyint,c integer,d bigint);
                        '''
        excute_cmd = f'''
                            source {self.DB_ENV_PATH} ;
                            gsql -d {self.primaryNode.db_name} -p {self.primaryNode.db_port} -c "{sql_cmd}" 
                            '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.TABLE_CREATE_SUCCESS, msg)

        # 备机查询
        logger.info('----------------------------备机查询-----------------------------')
        sql_cmd = 'select * from test_zl;'
        excute_cmd = f'''
                            source {self.DB_ENV_PATH} ;
                            gsql -d {self.standbyNode.db_name} -p {self.standbyNode.db_port} -c "{sql_cmd}" 
                        '''
        logger.info(excute_cmd)
        msg = self.standbyNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertNotIn(self.Constant.NO_FILE_MSG, msg)

        # 恢复参数值
        logger.info('----------------------------更改参数值-----------------------------')
        mod_msg = self.sh_primy.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'synchronous_commit = on')
        self.assertTrue(mod_msg)
        mod_msg1 = self.sh_primy.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'most_available_sync=off')
        self.assertTrue(mod_msg1)

        # 重启数据库使参数生效
        logger.info('----------------------------重启数据库使参数生效-----------------------------')
        is_stoped = self.sh_primy.stopDbCluster()
        self.assertTrue(is_stoped)
        logger.info(f'{is_stoped}')
        is_started = self.sh_primy.startDbCluster()
        self.assertTrue(is_started)
        logger.info(f'{is_started}')

    def tearDown(self):
        logger.info('----------------------------后置处理-----------------------------')
        try:
            db_status = self.sh_primy.getDbInstanceStatus()
            logger.info(db_status)
            if db_status:
                pass
            else:
                is_started = self.sh_primy.startDbCluster()
                self.assertTrue(is_started)
                logger.info(f'{is_started}')
                logger.info(f'db_status: {is_started}')
        finally:
            mod_msg = self.sh_primy.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'synchronous_commit = on')
            self.assertTrue(mod_msg)
            mod_msg1 = self.sh_primy.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, 'most_available_sync=off')
            self.assertTrue(mod_msg1)
            is_stoped = self.sh_primy.stopDbCluster()
            self.assertTrue(is_stoped)
            logger.info(f'{is_stoped}')
            is_started = self.sh_primy.startDbCluster()
            self.assertTrue(is_started)
            logger.info(f'{is_started}')
        logger.info('----------------------------Reliability_Dbsys_Case212执行完成-----------------------------')
